﻿using System;
using System.Collections.Generic;
using SqlSugar;
using WebApplication11.Helper;
using WebApplication11.Models;

namespace WebApplication11.Dao;

public class messageDao
{
    public static SqlSugarClient db = dbHelper.CretClient();
    public List<message> SelectAll()
    {
        int totalCount = 0;
        return db.Queryable<message>().ToPageList(1, 50, ref totalCount);
    }

    public List<message> Select(int id) {
        return db.Queryable<message>().Where(it => it.Id == id).ToList();
    }
        
    public List<message> Select(String leader) {
        return db.Queryable<message>().Where(it => it.Leader.Contains(leader)).ToList();
    }
    
    public bool Insert(message message) {
        try {
            db.Insertable(message).IgnoreColumns(false, true).ExecuteCommand();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
        
    public bool Delete(int aid) {
        try {
            db.Deleteable<message>().In(it => it.Id, aid).ExecuteCommand();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public bool Update(message message) {
        try {
            db.Updateable(message).IgnoreColumns(false, true).ExecuteCommand();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<message> search(int id)
    {
        return Select(id);
    }
    
    public List<message> search(string msg)
    {
        var res1 = db.Queryable<message>().Where(it => it.Leader.Contains(msg)).ToList();
        var res2 = db.Queryable<message>().Where(it => it.Name.Contains(msg)).ToList();
        res1.AddRange(res2);
        return res1;
    }
}